package com.swh.dao;

import com.swh.entity.Article;
import com.swh.entity.PageEntity;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/24 19:21
 * @Version: 1.0
 */
public class ArticleDao extends BaseDao {

    //文章搜索+分页(根据时间降序排序)
    public List<Article> findPage(String title, Integer startIndex, Integer pageSize) {
        List<Article> articleList = jdbcTemplate.query(
                "select * from article where title like ? order by publish_date desc limit ?,? ",
                new BeanPropertyRowMapper<>(Article.class),
                "%" + title + "%", startIndex, pageSize);

        return articleList;
    }

    //根据title查询总记录数
    public Integer countByTitle(String title) {
        try {
            return jdbcTemplate.queryForObject(
                    "select count(1) from article where title like ?",
                    Integer.class, "%" + title + "%");
        } catch (EmptyResultDataAccessException exception) {
            return null;
        }

    }

    public void add(Article article) {
        jdbcTemplate.update("insert into article values(null,?,?,?,?,?,?)",
                article.getTitle(),
                article.getContent(),
                article.getBrowseCount(),
                article.getPublishDate(),
                article.getPublishRealName(),
                article.getUserId()
        );
    }

    // 浏览量+1
    public void updateBrowserCount(Long id) {
        jdbcTemplate.update("update article set browse_count=browse_count+1 where id=?", id);
    }

    public Article findById(Long id) {
        try {
            return jdbcTemplate.queryForObject("select * from article where id=?", new BeanPropertyRowMapper<>(Article.class), id);
        } catch (EmptyResultDataAccessException exception) {
            return null;
        }

    }

    //收藏次数
    public Integer countFavoriteByArticleId(Long id) {
        try {
            return jdbcTemplate.queryForObject("select count(1) from favorite   where a_id=?", Integer.class, id);
        } catch (EmptyResultDataAccessException exception) {
            return null;
        }
    }

    //我是否收藏过的文章
    public Integer isFavorite(Long userId, Long articleId) {
        try {
            return jdbcTemplate.queryForObject("select count(*) from favorite where u_id=? and a_id=?", Integer.class, userId, articleId);
        } catch (EmptyResultDataAccessException exception) {
            return null;
        }
    }

    //已经收藏了；取消收藏
    public void delFunFavorite(Long id, long id1) {
        jdbcTemplate.update("delete from favorite where u_id=? and a_id=?", id, id1);

    }

    //还没有收藏；收藏
    public void addFavorite(Long id, long id1) {
        jdbcTemplate.update("insert into favorite values(?,?)", id, id1);
    }

    public List<Map<String, Object>> findUserFavorite(Long userId, Long articleId) {
        String sql = "        select u.id userId,u.real_name realName from userfocus uf\n" +
                "        left join user u on u.id=uf.user_focus_id\n" +
                "        left join favorite f on f.u_id=u.id\n" +
                "        where uf.user_id=? and f.a_id=?";
        return jdbcTemplate.queryForList(sql, userId, articleId);
    }


    public List<Article> favoritePage(Long userId, String title, Integer startIndex, Integer pageSize) {
        List<Article> articleList = jdbcTemplate.query(
                "select a.* from favorite f left join article a on f.a_id=a.id where f.u_id=? and title like ?  order by publish_date desc limit ?,?",
                new BeanPropertyRowMapper<>(Article.class),
                userId, "%" + title + "%", startIndex, pageSize);

        return articleList;
    }

    public Integer countByFavorite(Long userId, String title) {
        try {
            return jdbcTemplate.queryForObject(
                    "select count(1) from favorite f left join article a on f.a_id=a.id where f.u_id=? and title like ?",
                    Integer.class, userId, "%" + title + "%");

        } catch (EmptyResultDataAccessException exception) {
            return null;
        }
    }
}
